package com.ssh.service.impl;

import com.common.repostitory.CommonRepository;
import com.common.tool.Page;
import com.common.tool.PagingTool;
import com.common.tool.ZpenSequence;
import com.ssh.entity.Repair;
import com.common.repostitory.RepairRepository;
import com.ssh.service.RepairService;
import com.ssh.entity.User;
import com.ssh.requestPackage.RepairRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 文 件 名 称：RepairSerivceImpl.
 * 文 件 说 明：
 * 创   建   人：zpen.
 * 创 建 时 间：2018/3/1 17:33.
 */
@Service("repairService")
@Transactional
public class RepairServiceImpl implements RepairService {
    @Autowired
    CommonRepository commonRepository;
    @Autowired
    RepairRepository repairRepository;

    @Override
    public Long save(Repair repair,User user){
        Long faly = 0L;
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(user!=null){
                if(user.getUserType()==0){
                    repair.setOrderNo(ZpenSequence.getCode("CQ","ADM"));
                }else {
                    repair.setOrderNo(ZpenSequence.getCode("CQ","CON"));
                }
            }
            repair.setCreateTime(sf.parse(sf.format(System.currentTimeMillis())));
            repair.setConId(user.getId());
            repair.setOrderStatus("missed");
            faly=commonRepository.save(repair);
        }catch (Exception e){
            e.printStackTrace();
        }
        return faly;
    }

    public List<Map<String, Object>> findSql() {
        StringBuffer sql = new StringBuffer();//查询维修订单的sql
        sql.append("select * from repair ");

        return commonRepository.findSql(sql.toString());
    }

    public Repair getRepiarByProperty(String propertyName, Object value) {
        return (Repair) commonRepository.getEntityByProperty(Repair.class,propertyName,value);
    }

    public List<Map<String, Object>> geiRepiar(String name, String phone, String address, String expectRepairTime,
                                               String orderNo, String deviceType, String repairType, String repairTime,
                                               String createTime, String orderStatus, String completeName) {
        Object[] objects = new Object[]{name,phone,address,expectRepairTime,orderNo,deviceType,repairType,repairTime,createTime,orderStatus,completeName};
        StringBuffer sql = new StringBuffer();
        sql.append("select * from repair where 1=1");
        if(StringUtils.isBlank(name)){
            sql.append("and name = ?");
        }if(StringUtils.isBlank(phone)){
            sql.append("and phone = ?");
        }if(StringUtils.isBlank(address)){
            sql.append("and address = ?");
        }if(StringUtils.isBlank(expectRepairTime)){
            sql.append("and expect_repair_time = ?");
        }if(StringUtils.isBlank(orderNo)){
            sql.append("and order_no = ?");
        }if(StringUtils.isBlank(deviceType)){
            sql.append("and device_type = ?");
        }if(StringUtils.isBlank(repairType)){
            sql.append("and repair_type = ?");
        }if(StringUtils.isBlank(repairTime)){
            sql.append("and repair_time = ?");
        }if(StringUtils.isBlank(createTime)){
            sql.append("and create_time = ?");
        }if(StringUtils.isBlank(orderStatus)){
            sql.append("and order_status = ?");
        }if(StringUtils.isBlank(completeName)){
            sql.append("and complete_name = ?");
        }
        return commonRepository.findSql(sql.toString(),objects);
    }

    @Override
    public Repair getRepair(Repair repair) {
        return  commonRepository.getEntity(Repair.class,repair.getId());
    }

    @Override
    public PagingTool queryForPage(RepairRequest repairRequest,Page page) {
        PagingTool pagingTool = new PagingTool();
        pagingTool.setTotal(repairRepository.queryForAll(repairRequest,page));
        pagingTool.setPageSize(Integer.valueOf(page.getLimit()));
        pagingTool.setPageNo(Integer.valueOf(page.getOffset()));
        pagingTool.setRows(repairRepository.queryForPage(repairRequest,page));

        return pagingTool;
    }
}
